<?php

namespace App\Http\Controllers\Test;

use App\Http\Controllers\Controller;
use App\Service\Translate;
use App\Service\TranslateBaidu;
use App\Service\TranslateIts;
use App\Service\TranslateOts;
use Google\Cloud\Translate\V2\TranslateClient;


class TranslateController extends Controller
{

    // 翻译 测试
    public function initialize()
    {
        parent::initialize();
    }

    // 翻译
    // api.asiatechwire.com/test/translate/index
    public function index()
    {
        $text   = 'Hello world!';
        $text   = '<h1>Hello world!</h1>';
        $text   = $this->englishString();
        $result = $this->translateTo($text, 'cn');

        $translate = new TranslateClient(['key' => 'AIzaSyDB088_hCa5TvY_YcNpA0pC-U5qrJtVWj8']);
        $result2   = $translate->detectLanguage($text);
        var_dump($result2);


        return $this->success($result);
    }

    // 中文：谷歌翻译
    // api.asiatechwire.com/test/translate/china
    public function china()
    {
        $chinese   = $this->chineseString();
        $translate = new Translate();
        $result1   = $translate->translateTo($chinese, 'en');
        $result2   = $translate->translateTo($result1['text'], 'cn');
        $result3   = $translate->translateTo($result2['text'], 'en');
        $result4   = $translate->translateTo($result3['text'], 'cn');

        echo '<h2>原文</h2>';
        echo $chinese;
        echo '<h2>译文</h2>';
        echo $result2['text'];
        echo '<h2>译文</h2>';
        echo $result4['text'];
    }

    // 中文：讯飞
    // api.asiatechwire.com/test/translate/its-china
    public function otsChina()
    {
        $chinese   = $this->chineseString();
        $english   = $this->englishString();
        $translate = new TranslateIts();
        $translate = new TranslateOts();
        $result1   = $translate->xfyun($chinese, 'cn', 'en');
        //$result2   = $translate->translateTo($result1['text'], 'cn');
        //$result3   = $translate->translateTo($result2['text'], 'en');
        //$result4   = $translate->translateTo($result3['text'], 'cn');

        echo '<h2>原文</h2>';
        echo $chinese;
        echo '<h2>以前谷歌的翻译</h2>';
        echo $english;
        echo '<h2>讯飞现在的译文</h2>';
        //var_dump($result1);
        $res1 = json_decode($result1, true);
        if($res1['code'] == 0){
            $data = $res1['data']['result'];
            echo '源语种：'.$data['from'].'<br>';
            echo '目标语种：'.$data['to'].'<br>';
            //echo '源文本：'.$data['trans_result']['src'].'<br>';
            echo '目标文本：'.$data['trans_result']['dst'].'<br>';

        }

        echo '<h2>译文</h2>';
        //echo $result2['text'];
        echo '<h2>译文</h2>';
        //echo $result4['text'];
    }

    // 中文：讯飞
    // api.asiatechwire.com/test/translate/baidu-china
    public function baiduChina()
    {
        $chinese   = $this->chineseString();
        $english   = $this->englishString();
        $translate = new TranslateBaidu();
        $result1   = $translate->run($chinese, 'zh', 'en');
        //$result2   = $translate->translateTo($result1['text'], 'cn');
        //$result3   = $translate->translateTo($result2['text'], 'en');
        //$result4   = $translate->translateTo($result3['text'], 'cn');

        echo '<h2>原文</h2>';
        echo $chinese;
        echo '<h2>以前谷歌的翻译</h2>';
        echo $english;
        echo '<h2>百度现在的译文</h2>';
        //var_dump($result1);
        $res1 = json_decode($result1, true);
        if(empty($res1['error_msg'])){
            $data = $res1['result'];
            //var_dump($data);
            echo '源语种：'.$data['from'].'<br>';
            echo '目标语种：'.$data['to'].'<br>';
            //echo '源文本：'.$data['trans_result'][0]['src'].'<br>';
            echo '目标文本：'.$data['trans_result'][0]['dst'].'<br>';
        } else{
            echo '错误消息：'.$res1['error_msg'].'<br>';
        }

        echo '<h2>译文</h2>';
        //echo $result2['text'];
        echo '<h2>译文</h2>';
        //echo $result4['text'];
    }

    // 测试
    // api.asiatechwire.com/test/translate/translate
    public function translate()
    {
        $english = $this->englishString();
        $chinese = $this->chineseString();

        // 执行
        try{
            $translate = new Translate();
            $result2   = $translate->translateTo($chinese, 'en');
        } catch(\Exception $e){
            return $e->getMessage();
        }

        //$result1   = $translate->getLanguage($chinese);

        // 直接输出
        //echo '<h2>当前语言</h2>';
        //var_dump($result1);

        //echo '<h2>英文原文</h2>';
        //echo $english;
        //echo '<h2>中文原文</h2>';
        //echo $chinese;

        echo '<h2>原文</h2>';
        echo $result2['input'];
        echo '<h2>译文</h2>';
        echo $result2['text'];

        //return $this->success($result);
    }


    // 中文字符串 html
    protected function chineseString()
    {
        return <<<EOF
【康为世纪：子公司获幽门螺杆菌耐药基因检测试剂盒III类注册证】康为世纪公告，全资子公司江苏健为诊断科技有限公司自主研发生产的幽门螺杆菌23S rRNA/gyrA基因突变检测试剂盒收到国家药品监督管理局颁发的III类《医疗器械注册证》。该产品用于定性检测幽门螺杆菌阳性患者正常粪便样本中幽门螺杆菌23S rRNA基因A2142G、A2142C、A2143G突变和gyrA基因A260T、C261A、T261G、G271A、G271T、A272G突变。注册证编号为国械注准20243402348，注册分类为境内第三类体外诊断试剂，注册证有效期至2029年11月20日。
EOF;
    }

    // 中文字符串 html
    protected function chineseString2()
    {
        return <<<EOF
<p style="padding:3px 0;"><img style="width: 1%;" src="http://www.asia.com/asia/uploads/2021/04/Zhang-Zhongmou-edited.jpg" /></p><p style="padding:3px 0;"><strong>亚洲科技讯</strong>（11 月 7 日）——据台湾《经济日报》报道，台积电创始人张忠谋已向英伟达创始人黄仁勋询问他对担任台积电首席执行官一职的兴趣。</p><p style="padding: 3px 0;">张忠谋自传的下一本将于11月29日发售。据获得该自传授权的台湾出版商天下出版称，张忠谋曾询问黄仁勋是否有兴趣接任台积电首席执行官。</p><p style="padding: 3px 0;">这位93岁的中国企业家自2005年起就一直在规划自己的继任者，并于2012年任命了三位联席首席运营官，分别是刘德华、魏则仕和蒋尚义，打算从三人中选择一人来接替他。</p><p style="padding: 3px 0;">然而，2013年，张忠谋私下对于谁来接替他担任台积电首席执行官有不同的想法。</p><p style="padding: 3px 0;">在下一卷自传中，张忠谋提到，他曾问过黄仁勋是否愿意接任台积电下一任首席执行官。</p><p style="padding: 3px 0;">不过黄仁勋并没有同意这个提议，继续领导Nvidia。</p><p style="padding: 3px 0;"> Nvidia 与台积电合作多年，黄仁勋与张忠谋多年来一直是好朋友。</p>
EOF;
    }

    // 英文字符串 html
    protected function englishString()
    {
        return <<<EOF
<p style="padding: 3px 0;"><img style="width: 1%;" src="http://www.asia.com/asia/uploads/2021/04/Zhang-Zhongmou-edited.jpg" /></p><p style="padding: 3px 0;"><strong>Asia Tech Wire</strong> (Nov 7) -- TSMC founder Morris Chang has asked Nvidia founder Jensen Huang about his interest in the TSMC CEO post, according to Taiwan's Economic Daily News.</p><p style="padding: 3px 0;">The next volume of Morris Chang's autobiography will be available on November 29th. According to Commonwealth, the Taiwanese publisher that acquired the license for the autobiography, Chang had asked Huang if he was interested in taking over as TSMC's CEO.</p><p style="padding: 3px 0;">The 93-year-old Chinese entrepreneur has been planning his succession since 2005, and in 2012 appointed three co-chief operating officers, Mark Liu, C.C. Wei and Chiang Shang-yi, with the intention of choosing one of the three to succeed him.</p><p style="padding: 3px 0;">However, in 2013, Chang privately had a different idea of who he wanted to succeed him as TSMC CEO.</p><p style="padding: 3px 0;">In the next volume of his autobiography, Chang mentioned that he had asked Huang if he would be willing to take over as TSMC's next CEO.</p><p style="padding: 3px 0;">However, Huang did not agree to the proposal and continued to lead Nvidia.</p><p style="padding: 3px 0;">Nvidia has been working with TSMC for many years, and Huang and Chang have been good friends for years.</p>
EOF;
    }

}


